<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="128px"
    >
    <el-form-item label="演练标题" label-width='80px'>
        <el-input
          v-model="queryParams.emName"
          placeholder="请输入演练标题"
          clearable
           @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    <!-- <el-form-item label="演练时间" prop="startTime">
        <el-date-picker
          v-model="bnTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item> -->

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-dowlond"
          size="mini"
          @click="getCode"
          >下载二维码</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-tabs v-model="activeName">
      <el-tab-pane label="未发布方案" name="first">
        <el-table
          v-loading="loading"
          :data="unitList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="演练标题" align="center" prop="emName" />
          <el-table-column label="演练时间" align="center" prop="startTime" />
          <el-table-column label="演练部门" align="center" prop="deptName" />
          <el-table-column label="方案文件" align="center" prop="programme">
            <template slot-scope="scope">
              <div v-if="scope.row.draftUrl">
                <el-button
                  v-if="gettp(scope.row.draftUrl) == 1"
                  size="mini"
                  type="text"
                  @click="look(scope.row.draftUrl)"
                  >查看</el-button
                >
                <el-button
                  v-if="gettp(scope.row.draftUrl) == 2"
                  size="mini"
                  type="text"
                  @click="download(scope.row.draftUrl)"
                  >下载</el-button
                >
              </div>
            </template>
          </el-table-column>
          <!-- <el-table-column label="演习现场规则" align="center" prop="rule" /> -->
          <!-- <el-table-column label="外包状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status == 1 ? "进行中" : "未进行" }}</span>
        </template>
      </el-table-column> -->
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="send(scope.row)"
                v-hasPermi="['app:emerg:edit']"
                >发布</el-button
              >
              <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:unit:remove']"
            >删除</el-button
          > -->
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="ListVehiclestotal > 0"
          :total="ListVehiclestotal"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
      <el-tab-pane label="已发布" name="second">
        <el-table v-loading="loading2" :data="sendList">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="演练标题" align="center" prop="emName" />
          <el-table-column label="演练时间" align="center" prop="startTime" />
          <el-table-column
            label="演练方案说明"
            align="center"
            prop="programme"
          />
          <el-table-column label="方案文件" align="center" prop="programme">
            <template slot-scope="scope">
              <div v-if="scope.row.draftUrl">
                <el-button
                  v-if="gettp(scope.row.draftUrl) == 1"
                  size="mini"
                  type="text"
                  @click="look(scope.row.draftUrl)"
                  >查看</el-button
                >
                <el-button
                  v-if="gettp(scope.row.draftUrl) == 2"
                  size="mini"
                  type="text"
                  @click="download(scope.row.draftUrl)"
                  >下载</el-button
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column label="通知文件" align="center" prop="programme">
            <template slot-scope="scope">
              <div v-if="scope.row.note">
                <el-button
                  v-if="gettp(scope.row.note) == 1"
                  size="mini"
                  type="text"
                  @click="look(scope.row.note)"
                  >查看</el-button
                >
                <el-button
                  v-if="gettp(scope.row.note) == 2"
                  size="mini"
                  type="text"
                  @click="download(scope.row.note)"
                  >下载</el-button
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column label="演习现场规则" align="center" prop="rule" />
          <!-- <el-table-column label="外包状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status == 1 ? "进行中" : "未进行" }}</span>
        </template>
      </el-table-column> -->
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="Details(scope.row.flowDetails)"
                >查看详细</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total2 > 0"
          :total="total2"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
    </el-tabs>

    <!-- 添加或修改外包公司名称对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="通知内容" prop="companyName">
          <el-input v-model="form.noteContent" placeholder="通知内容" />
        </el-form-item>
        <el-form-item label="通知文件" prop="companyName">
          <FileUpload
            v-model="form.note"
            @input="draftUrlupImage"
            :limit="5"
          ></FileUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- <el-dialog title="演练详情" :visible.sync="lookModel" width="800px" append-to-body>
        <el-divider>参会人员</el-divider>
        
        <el-divider>参与部门</el-divider>

        <el-divider>参与人员</el-divider>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="lookModel = false">取 消</el-button>
      </div>
    </el-dialog> -->
    <el-dialog
      title="详情"
      :visible.sync="detailOpen"
      width="1100px"
      append-to-body
    >
      <el-table :data="ditailList">
        <el-table-column label="审核人" align="center" prop="name" />
        <el-table-column label="审核顺序" align="center" prop="sort" />
        <el-table-column label="审核状态" align="center" prop="status">
          <template slot-scope="scope">
            {{ myStatus[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column label="审核意见" align="center" prop="content">
        </el-table-column>
        <el-table-column label="审核时间" align="center" prop="auditTime" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="签名" align="center" prop="sign">
          <template slot-scope="scope">
            <!-- {{scope.row.sign}} -->
            <img v-if="scope.row.sign" style="display:block;width:80px;height:40px" :src="scope.row.sign" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <preview-content v-if="showType" :previewMsg="previewMsg"></preview-content>
  </div>
</template>
  
  <script>
import {
  listUnit,
  getUnit,
  delUnit,
  addUnit,
  updateUnit,
} from "@/api/system/outunit";
import {
  listVehicles,
  flowList,
  listByMyslef,
  vehicles,
  vehiclesQuery,
  myVehicles,
  vehiclesUpdate,
  emergSend,
  getCode,
  emergUpdateNote,
} from "@/api/system/emerg";
import { downloadFile } from "@/utils/download.js";
import PreviewContent from "@/components/PreviewContent/PreviewContent.vue";
import cache from "@/plugins/cache";
export default {
  name: "Unit",
  components: {
    PreviewContent,
  },
  data() {
    return {
        bnTime:[],
        myStatus: {
        1: "审核中",
        2: "待我审核",
        3: "通过",
        4: "驳回",
      },
      detailStatus: {
        // 1: "待审核",
        3: "通过",
        4: "驳回",
        // 4: "撤销",
      },
        detailOpen: false,
        lookModel:false,
      showType: true,
      previewMsg: {
        dialogTableVisible: false,
        fileType: "",
        iframeUrl: "",
      },
      activeName: "first",
      userId: cache.session.getJSON("userId"),
      // 遮罩层
      loading: true,
      ListVehiclestotal: 0,
      loading2: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total2: 0,
      // 未发布
      unitList: [],
      //   已发布
      sendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: this.userId,
        type: 7,
        approStatus: 2,
        noteFlag: 0,
        beginStartTime:null,
        endStartTime:null,
        emName:null
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        userId: this.userId,
        type: 7,
        approStatus: 2,
        noteFlag: 1,
        beginStartTime:null,
        endStartTime:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      ditailList: [],
    };
  },
  created() {
    this.getList();
    this.getsendList();
    this.reset();
  },
  methods: {
    Details(data) {
      console.log(data);
      this.detailOpen = true;
      this.ditailList = data;
    },
    draftUrlupImage(e) {
      this.form.note = e;
    },
    /** 查询外包公司名称列表 */
    getList() {
      this.loading = true;
      this.queryParams.userId = this.userId;
      this.queryParams.beginStartTime = this.bnTime[0]
      this.queryParams.endStartTime = this.bnTime[1]
      emergSend(this.queryParams).then((response) => {
        this.unitList = response.rows;
        this.ListVehiclestotal = response.total;
        this.loading = false;
      });
    },
    send(data) {
      this.open = true;
      this.form.sumId = data.sumId;
      this.form.flowNo = data.flowNo;
    },
    getCode() {
      getCode();
    },
    getsendList() {
      this.loading2 = true;
      this.queryParams2.userId = this.userId;
      this.queryParams2.beginStartTime = this.bnTime[0]
        this.queryParams2.endStartTime = this.bnTime[1]
      emergSend(this.queryParams2).then((response) => {
        this.sendList = response.rows;
        this.total2 = response.total;
        this.loading2 = false;
      });
    },
    lookList(){
        this.lookModel = true
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sumId: null,
        flowNo: null,
        noteFlag: 1, //发布
        note: null, // 通知文件
        noteContent: null, // 通知内容
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
       // this.bnTime = [];
       this.resetForm("queryForm");
    // this.queryParams = {
    //     pageNum: 1,
    //     pageSize: 10,
    //     beginStartTime: null,
    //     endStartTime: null,
    //     emName:null
    //   },
    this.queryParams.emName=null
      this.handleQuery();
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.companyId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加外包公司名称";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const companyId = row.companyId || this.ids;
      getUnit(companyId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改外包公司名称";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //   if (this.form.companyId != null) {
          //     updateUnit(this.form).then((response) => {
          //       this.$modal.msgSuccess("修改成功");
          //       this.open = false;
          //       this.getList();
          //     });
          //   } else {
          console.log(this.form);
          emergUpdateNote(this.form).then((response) => {
            this.$modal.msgSuccess("发布成功");
            this.open = false;
            this.getList();
            this.getsendList()
          });
          //   }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const companyIds = row.companyId || this.ids;
      this.$modal
        .confirm('是否确认删除外包公司名称编号为"' + companyIds + '"的数据项？')
        .then(function () {
          return delUnit(companyIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    download(value) {
      if (value.indexOf(",") >= 0) {
        let split = value.split(",");
        for (var i = 0; i < split.length; i++) {
          if (split[i] != "") {
            let num = split[i].lastIndexOf(".");
            let dis = split[i].substring(num + 1, num + 4);

            if (dis == "pdf" || dis == "PDF") {
              downloadFile(split[i], "a");
            } else {
              downloadFile(split[i], "iframe");
            }
          }
        }
      } else {
        let num = value.lastIndexOf(".");
        let dis = value.substring(num + 1, num + 4);
        if (dis == "pdf" || dis == "PDF") {
          downloadFile(value, "a");
        } else {
          downloadFile(value, "iframe");
        }
      }
    },
    gettp(v) {
      let type = null;
      if (v.includes(",")) {
        let list = v.split(",");
        list.forEach((item) => {
          type = this.getFileType(item);
        });
      } else {
        type = this.getFileType(v);
      }
      if (type == "pdf" || type == "txt") {
        return 1;
      } else {
        return 2;
      }
    },
    getFileType(url) {
      let pdfReg = /^.+(\.pdf)$/;
      let txtReg = /^.+(\.txt)$/;
      let wordReg = /^.+(\.doc|\.docx)$/;
      let excelReg = /^.+(\.xls|\.xlsx)$/;
      let jpgReg = /^.+(\.png|\.jpg|\.jpeg|\.bmp)$/;
      if (pdfReg.test(url)) {
        return "pdf";
      }
      if (txtReg.test(url)) {
        return "txt";
      }
      if (wordReg.test(url)) {
        return "word";
      }
      if (excelReg.test(url)) {
        return "excel";
      }
      if (jpgReg.test(url)) {
        return "picture";
      }
    },
    look(v) {
      this.showType = false;
      this.$nextTick(() => {
        this.showType = true;
      });
      let list = v.split(",");
      this.previewMsg.dialogTableVisible = true;
      this.previewMsg.iframeUrl = list[0];
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/unit/export",
        {
          ...this.queryParams,
        },
        `unit_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  